/*     */ package com.zimbra.cs.index;
/*     */ 
/*     */ import com.google.common.base.Strings;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.index.analysis.AddrCharTokenizer;
/*     */ import com.zimbra.cs.index.analysis.ContactTokenFilter;
/*     */ import com.zimbra.cs.index.analysis.FilenameTokenizer;
/*     */ import com.zimbra.cs.index.analysis.NumberTokenizer;
/*     */ import com.zimbra.cs.index.analysis.UniversalAnalyzer;
/*     */ import java.io.IOException;
/*     */ import java.io.Reader;
/*     */ import java.io.StringReader;
/*     */ import java.util.Map;
/*     */ import java.util.concurrent.ConcurrentHashMap;
/*     */ import org.apache.lucene.analysis.Analyzer;
/*     */ import org.apache.lucene.analysis.KeywordTokenizer;
/*     */ import org.apache.lucene.analysis.TokenStream;
/*     */ import org.apache.lucene.analysis.standard.StandardAnalyzer;
/*     */ import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class ZimbraAnalyzer
/*     */   extends Analyzer
/*     */ {
/*  51 */   private static final ZimbraAnalyzer SINGLETON = new ZimbraAnalyzer();
/*  52 */   private static final Map<String, Analyzer> ANALYZERS = new ConcurrentHashMap();
/*     */   
/*  54 */   static { ANALYZERS.put("StandardAnalyzer", new ForwardingAnalyzer(new StandardAnalyzer(LuceneIndex.VERSION))); }
/*     */   
/*     */ 
/*  57 */   private final Analyzer defaultAnalyzer = new UniversalAnalyzer();
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Analyzer getAnalyzer(String name)
/*     */   {
/*  68 */     if (Strings.isNullOrEmpty(name)) {
/*  69 */       return SINGLETON;
/*     */     }
/*  71 */     Analyzer result = (Analyzer)ANALYZERS.get(name);
/*  72 */     return result != null ? result : SINGLETON;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Analyzer getInstance()
/*     */   {
/*  81 */     return SINGLETON;
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static void registerAnalyzer(String name, Analyzer analyzer)
/*     */     throws ServiceException
/*     */   {
/*  97 */     if (ANALYZERS.containsKey(name)) {
/*  98 */       throw ServiceException.FAILURE("Cannot register analyzer: " + name + " because there is one already registered with that name.", null);
/*     */     }
/*     */     
/* 101 */     ANALYZERS.put(name, analyzer);
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */   public static void unregisterAnalyzer(String name)
/*     */   {
/* 108 */     ANALYZERS.remove(name);
/*     */   }
/*     */   
/*     */   public static String getAllTokensConcatenated(String fieldName, String text) {
/* 112 */     return getAllTokensConcatenated(fieldName, new StringReader(text));
/*     */   }
/*     */   
/*     */   public static String getAllTokensConcatenated(String fieldName, Reader reader) {
/* 116 */     StringBuilder toReturn = new StringBuilder();
/*     */     
/* 118 */     TokenStream stream = SINGLETON.tokenStream(fieldName, reader);
/* 119 */     CharTermAttribute term = (CharTermAttribute)stream.addAttribute(CharTermAttribute.class);
/*     */     try
/*     */     {
/* 122 */       stream.reset();
/* 123 */       while (stream.incrementToken()) {
/* 124 */         toReturn.append(term);
/* 125 */         toReturn.append(' ');
/*     */       }
/* 127 */       stream.end();
/* 128 */       stream.close();
/*     */     } catch (IOException e) {
/* 130 */       e.printStackTrace();
/*     */     }
/*     */     
/* 133 */     return toReturn.toString();
/*     */   }
/*     */   
/*     */   public TokenStream tokenStream(String field, Reader reader)
/*     */   {
/* 138 */     return tokenStream(field, reader, this.defaultAnalyzer);
/*     */   }
/*     */   
/*     */   private TokenStream tokenStream(String field, Reader reader, Analyzer analyzer) {
/* 142 */     if (field.equals("msg_id"))
/* 143 */       return new KeywordTokenizer(reader);
/* 144 */     if ((field.equals("attachment")) || (field.equals("type")))
/* 145 */       throw new IllegalArgumentException("Use MimeTypeTokenStream");
/* 146 */     if (field.equals("l.size"))
/* 147 */       return new NumberTokenizer(reader);
/* 148 */     if ((field.equals("from")) || (field.equals("to")) || (field.equals("cc")) || (field.equals("env_from")) || (field.equals("env_to")))
/*     */     {
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/* 156 */       return new AddrCharTokenizer(reader); }
/* 157 */     if (field.equals("l.contactData"))
/* 158 */       return new ContactTokenFilter(new AddrCharTokenizer(reader));
/* 159 */     if (field.equals("filename")) {
/* 160 */       return new FilenameTokenizer(reader);
/*     */     }
/* 162 */     return analyzer.tokenStream(field, reader);
/*     */   }
/*     */   
/*     */   public static final TokenStream getTokenStream(String field, Reader reader)
/*     */   {
/* 167 */     return SINGLETON.tokenStream(field, reader);
/*     */   }
/*     */   
/*     */   public TokenStream reusableTokenStream(String field, Reader reader)
/*     */   {
/* 172 */     return tokenStream(field, reader);
/*     */   }
/*     */   
/*     */   private static final class ForwardingAnalyzer extends Analyzer {
/*     */     private final Analyzer forwarding;
/*     */     
/*     */     ForwardingAnalyzer(Analyzer analyzer) {
/* 179 */       this.forwarding = analyzer;
/*     */     }
/*     */     
/*     */     public TokenStream tokenStream(String field, Reader reader)
/*     */     {
/* 184 */       return ZimbraAnalyzer.SINGLETON.tokenStream(field, reader, this.forwarding);
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/ZimbraAnalyzer.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */